<?
class Component {
	private $componentName;
	private $templateName;
	private $arParams;
	private $arResult;
	private $PARAMS;
	private $nameSpace;

	public static function getComponent($componentName, $templateName, $arComponentParams) {
		$component = new Component();
		$component->startComponent($componentName, $templateName, $arComponentParams);
	}

    public function startComponent($componentName, $templateName, $arComponentParams) {
		global $CTLANG, $USER;

		$CTLANG = Array();
		$this->arResult = Array();

		if($templateName == "") {
            $templateName = "main";
        }

		if (strstr($componentName, ":")) {
			$arCmpName = explode(':', $componentName);
			$componentNameSpace	=	$arCmpName[0];
			$componentName		=	$arCmpName[1];
		} else {
			$componentNameSpace	=	"system";
		}

		$this->nameSpace		= strtolower($componentNameSpace);
		$this->componentName	= strtolower($componentName);
		$this->templateName		= strtolower($templateName);
		$this->arParams			= $arComponentParams;

		if (is_file(TPL_PATH . 'components/'. $this->nameSpace . "/" . $this->componentName . "/" . $this->templateName . "/template.php")) {
			$tpl = TPL_PATH . 'components/'. $this->nameSpace . "/" . $this->componentName . "/" . $this->templateName . "/";
		} else {
			$tpl = COMPONENT_PATH . $this->nameSpace . "/" . $this->componentName . "/templates/" . $this->templateName . "/";
		}

		$this->PARAMS = Array(
			"COMPONENT" => Array (
				"NAME" => $name,
				"PATH" => COMPONENT_PATH . $this->nameSpace . "/" . $this->componentName . "/"
			),
			"TEMPLATE" => Array (
				"NAME" => $tplName,
				"PATH" => $tpl
			),
			
		);

		if (defined('DISPLAY_MODE') AND DISPLAY_MODE === 'EDIT') {
			Face::ShowComponentHead(Array(
				'NAME' => $componentName,
				'TPL' => empty($templateName) ? 'main' : $templateName,
				'CMP_PATH' => str_replace($_SERVER["DOCUMENT_ROOT"], '', $this->PARAMS["COMPONENT"]["PATH"]),
				'TPL_PATH' => str_replace($_SERVER["DOCUMENT_ROOT"], '', $this->PARAMS["TEMPLATE"]["PATH"]),
				'PARAMETERS' => $this->arParams
			));
		}

		$langFilesPath = $this->PARAMS["COMPONENT"]["PATH"] . "lang/" . LANG_ID . "/";
		if (is_dir($langFilesPath)) {
			getLangFilesLocale($langFilesPath);
		}

        if (file_exists($this->PARAMS["COMPONENT"]["PATH"] . "component.php")) {
            $arParams = Array();
			$arResult = Array();
            $arParams = $this->arParams;
			if (file_exists($this->PARAMS["TEMPLATE"]["PATH"] . "before.php"))
				include $this->PARAMS["TEMPLATE"]["PATH"] . "before.php";
            include $this->PARAMS["COMPONENT"]["PATH"] . "component.php";

			if (defined('DISPLAY_MODE') AND DISPLAY_MODE === 'EDIT') {
				Face::ShowComponentFoot();
			}

        } else {
			ShowError('Ошибка: Компонент "<b>'.$this->nameSpace.':'.$this->componentName.'</b>" не найден!');
		}
    }
	
    private function getComponentTemplate($arResult = Array()) {
		global $CTLANG, $USER;
		$this->arResult = $arResult;
		
		$arParams = Array();
		$arParams = $this->arParams;

		$arResult = Array();
		$arResult = $this->arResult;

		if (file_exists($this->PARAMS["TEMPLATE"]["PATH"] . "after.php"))
			include $this->PARAMS["TEMPLATE"]["PATH"] . "after.php";

		// Подрубаем язык шаблона
		$langFilesPath = $this->PARAMS["TEMPLATE"]["PATH"] . "lang/" . LANG_ID . "/";
		if (is_dir($langFilesPath)) {
			getLangFilesLocale($langFilesPath);
		}

		// Подрубаем шаблон
		if (file_exists($this->PARAMS["TEMPLATE"]["PATH"] . "/template.php")) {
			include $this->PARAMS["TEMPLATE"]["PATH"] . "/template.php";
        } else {
			ShowError('Ошибка: Шаблон "<b>'.$this->templateName.'</b>" компонента "<b>'.$this->nameSpace.':'.$this->componentName.'</b>" не найден!');
		}

		$arResult = Array();
		$arParams = Array();
    }

	public static function GetParametersForm($path, $tpl, $arParams) {
		$arComponentParameters = self::GetParameters($path, $tpl, $arParams);

        $form = '<form class="dialog-window-form-form">';

        $form .= '<table class="dialog-window-form-table styled">';

        if (!empty($arComponentParameters)) {
            foreach ($arComponentParameters['PARAMETERS'] as $name => $parameter) {
                $form .= Face::GetParameterHTML($name, $parameter, $arParams[$name]);
            }
        } else {
            $form .= '<tr><td>Настроек не найдено</td></tr>';
        }

        $form .= '</table>';
        $form .= '</form>';

        return $form;

	}

	public static function GetParameters($path, $tpl, $arExistingValues) {
		$arParameters = Array();
        $arComponentParameters = Array();
        $arTemplateParameters = Array();
		
		if (is_file($_SERVER["DOCUMENT_ROOT"] . $path . '.parameters.php')) {
			include $_SERVER["DOCUMENT_ROOT"] . $path . '.parameters.php';
			$arParameters = array_merge($arParameters, $arComponentParameters);
		}

		if (is_file($_SERVER["DOCUMENT_ROOT"] . $tpl . '.parameters.php')) {
			include $_SERVER["DOCUMENT_ROOT"] . $tpl . '.parameters.php';
			$arParameters = array_merge($arParameters, $arTemplateParameters);
		}
		$arParameters = array_merge($arParameters, $arComponentParameters);

		return $arParameters;
	}
}

?>